package Dao;

import entity.Mail;
import entity.User;
import utils.JdbcUtils_DBCP;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * @className: AdminDaoImpl
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/30
 **/
public class AdminDaoImpl implements AdminDao {
    @Override
    public String login(String id) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "SELECT `adminpassword` FROM `adminUser` WHERE adminUserID=?";
            //预编译sql 先写sql 但是不执行
            st = conn.prepareStatement(sql);
            //手动给参数赋值

            st.setString(1, id);


            //以上填充完毕 然后执行
            re = st.executeQuery();
            if (re.next()) {
                return re.getString("adminpassword");
            } else {
                return null;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }

    @Override
    public List<Mail> queryAllMail() {
        List<Mail> Mails = new ArrayList<Mail>();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "select * from mail ;";
            st = conn.prepareStatement(sql);
            re = st.executeQuery();
            while (re.next()) {
                String mid = re.getString("mid");
                String uid = re.getString("uid");
                String mName = re.getString("mname");
                String arrivalTime = re.getString("arrivaltime");
                String CompletionTime = re.getString("Completiontime");
                String mailState = re.getString("mailstate");
                Mail mail = new Mail(mid, uid, mName, arrivalTime, CompletionTime, mailState);
                Mails.add(mail);
            }
            return Mails;
        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }

    @Override
    public List<Mail> queryMail(String mId) {
        List<Mail> Mails = new ArrayList<Mail>();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "select * from mail Where mid = ? ;";
            st = conn.prepareStatement(sql);
            st.setString(1, mId);
            re = st.executeQuery();
            while (re.next()) {
                String uid = re.getString("uid");
                String mName = re.getString("mname");
                String arrivalTime = re.getString("arrivaltime");
                String CompletionTime = re.getString("Completiontime");
                String mailState = re.getString("mailstate");
                Mail mail = new Mail(mId, uid, mName, arrivalTime, CompletionTime, mailState);
                Mails.add(mail);
            }
            return Mails;
        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }

    @Override
    public List<User> queryAllUser() {
        List<User> Users = new ArrayList<User>();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "select * from user ;";
            st = conn.prepareStatement(sql);
            re = st.executeQuery();
            while (re.next()) {
                String uid = re.getString("uid");
                String password = re.getString("password");
                String IdCard = re.getString("IdCard");
                String sex = re.getString("sex");
                int age = re.getInt("age");
                String address = re.getString("address");
                String uname = re.getString("uname");
                String Telephone = re.getString("Telephone");
                User User = new User(uid, password, IdCard, sex, age, address,uname,Telephone);
                Users.add(User);
            }
            return Users;
        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }

    @Override
    public User queryUser(String uid) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        User user = null;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "select * from user Where uid = ? ;";
            st = conn.prepareStatement(sql);
            st.setString(1, uid);
            re = st.executeQuery();
            while (re.next()) {
                String password = re.getString("password");
                String Idcard = re.getString("Idcard");
                String sex = re.getString("sex");
                int age = re.getInt("age");
                String address = re.getString("address");
                String uname = re.getString("uname");
                String Telephone = re.getString("Telephone");

                 user = new User(uid, password, Idcard, sex, age, address,uname,Telephone);

            }
            return user;
        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }


    @Override
    public boolean modifyUser(String uId, String password, String idCard, String sex, int age, String address, String uName, String telephone) {
        Connection conn = null;
        PreparedStatement st = null;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "    UPDATE `user` SET `password`=?,`Idcard`=?,`sex`=?,`age`=?,`address`=?,`uname`=?,`Telephone`=? Where uid=?;";
            st = conn.prepareStatement(sql);
            //手动给参数赋值

            st.setString(8, uId);
            st.setString(1, password);
            st.setString(2, idCard);
            st.setString(3, sex);
            st.setInt(4, age);
            st.setString(5, address);
            st.setString(6, uName);
            st.setString(7, telephone);
            //以上填充完毕 然后执行
            int i = st.executeUpdate();
            if (i > 0) {
                System.out.println("用户信息修改成功");
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            System.out.println("SQLException");
            return false;
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
    }

    @Override
    public boolean deleteUser(String uid) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "delete FROM `user` WHERE uid=?;";
            //预编译sql 先写sql 但是不执行
            st = conn.prepareStatement(sql);
            //手动给参数赋值

            st.setString(1, uid);


            //以上填充完毕 然后执行
            int i = st.executeUpdate();
            if (i>0){
                return true;
            }else {
                return false;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return false;
    }

    @Override
    public List<Mail> withoutDeliveryForThreeDays() {
        List<Mail> withoutDeliveryMails = new ArrayList<Mail>();

        Connection conn = null;
        PreparedStatement st = null;
        ResultSet re;
        try {
            Date date = new java.util.Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            long curTime = date.getTime();

            conn = JdbcUtils_DBCP.getConnection();
            //?是占位符 代替参数
            String sql = "select * from mail where mailstate='未派送';";
            st = conn.prepareStatement(sql);
            re = st.executeQuery();
            while (re.next()) {
                String mid = re.getString("mid");
                String uid = re.getString("uid");
                String mName = re.getString("mname");
                String arrivalTime = re.getString("arrivaltime");
                String CompletionTime = re.getString("Completiontime");
                String mailState = re.getString("mailstate");
                Mail mail = new Mail(mid, uid, mName, arrivalTime, CompletionTime, mailState);
                String arrivalTime1 = mail.getArrivalTime();
                Date arrivalDate = dateFormat.parse(arrivalTime1);
                long aTime = arrivalDate.getTime();
                Long num =curTime-aTime;//时间戳相差的毫秒数
                long l = num / 24 / 60 / 60 / 1000;
                if (l>3){
                    withoutDeliveryMails.add(mail);
                }
            }
            return withoutDeliveryMails;

        } catch (SQLException e) {
            System.out.println("SQLException");
            e.printStackTrace();
        } catch (ParseException e) {
            System.out.println("ParseException");
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.release(conn, st, null);
        }
        return null;
    }




}

